home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / ORBMECHD / PSTR_TO_.C < prev   
Text File  |  1990-09-20  |  5KB  |  272 lines

  1. /************************************************
  2.  
  3. Program to convert a Pascal string to an extended
  4. precision real number.
  5.  
  6. 7 May 90
  7.  
  8. ************************************************/
  9.  
  10. #include    "orbmech.h"
  11. #include    "SANE.h"
  12. #include    "convert.h"
  13.  
  14. /*******************************************/        
  15.         
  16. extended    ext_pow( x, y )
  17. extended        x, y;
  18. {
  19.     int            i, j; 
  20.     extended  value;
  21.     
  22.     value = x;
  23.     
  24.     for ( i = 2; i <= y; i++ ) 
  25.         value *= x;
  26.         
  27.     return (value);
  28. }
  29.  
  30. /*******************************************/        
  31.         
  32. Pstr_to_extended( The_str, return_number, success )
  33. Str255        The_str;
  34. extended    *return_number;
  35. Boolean        *success;
  36. {
  37.     extended    sig_fig[30], abs_p_d;
  38.     int            exp_fig[10], dec_pos = 0, exp_size = 0, sig_size = 0, l = 0;
  39.     int            abs_place = 0, i = 0, j = 0, temp = 0, exp_value = 0, k = 0;
  40.     Boolean        exp_start = 0, dec_found = 0, sig_neg = 0, exp_neg = 0;
  41.     char        temp_char;
  42.     
  43.     temp = The_str[0];
  44.     
  45.     *success = 1;
  46.     
  47.     for( i = 1; i <= temp; i++)   {
  48.         
  49.         temp_char = The_str[i];
  50.         
  51.         if (sig_size > 30) {
  52.             Alert( SIG_OVERFLOW, NIL_POINTER );
  53.             *success = 0;
  54.             break;
  55.         }
  56.         
  57.         if (exp_size > 10) {
  58.             Alert( EXP_OVERFLOW, NIL_POINTER );
  59.             *success = 0;
  60.             break;
  61.         }
  62.  
  63.         if ( !exp_start )  {
  64.             switch ( temp_char )  {
  65.                 case ZERO:
  66.                     sig_size++;
  67.                     sig_fig[sig_size] = 0.0;
  68.                     break;
  69.                 case ONE:
  70.                     sig_size++;
  71.                     sig_fig[sig_size] = 1.0;
  72.                     break;
  73.                 case TWO:
  74.                     sig_size++;
  75.                     sig_fig[sig_size] = 2.0;
  76.                     break;
  77.                 case THREE:
  78.                     sig_size++;
  79.                     sig_fig[sig_size] = 3.0;
  80.                     break;
  81.                 case FOUR:
  82.                     sig_size++;
  83.                     sig_fig[sig_size] = 4.0;
  84.                     break;
  85.                 case FIVE:
  86.                     sig_size++;
  87.                     sig_fig[sig_size] = 5.0;
  88.                     break;
  89.                 case SIX:
  90.                     sig_size++;
  91.                     sig_fig[sig_size] = 6.0;
  92.                     break;
  93.                 case SEVEN:
  94.                     sig_size++;
  95.                     sig_fig[sig_size] = 7.0;
  96.                     break;
  97.                 case EIGHT:
  98.                     sig_size++;
  99.                     sig_fig[sig_size] = 8.0;
  100.                     break;
  101.                 case NINE:
  102.                     sig_size++;
  103.                     sig_fig[sig_size] = 9.0;
  104.                     break;
  105.                 case POINT:
  106.                     if (dec_found )  {
  107.                         *success = 0;
  108.                         Alert( MULT_PT, NIL_POINTER );
  109.                         break;
  110.                     }
  111.                     else  {
  112.                         dec_pos = sig_size;
  113.                         dec_found = 1;
  114.                     }
  115.                 case SPACE:
  116.                     continue;
  117.                     break;
  118.                 case COMMA:
  119.                     continue;
  120.                     break;
  121.                 case LET_D:
  122.                     exp_start = 1;
  123.                     break;
  124.                 case LET_E:
  125.                     exp_start = 1;
  126.                     break;
  127.                 case MINUS:
  128.                     if ( i == 1 ) 
  129.                         sig_neg = 1;
  130.                     else  {
  131.                         *success = 0;
  132.                         Alert( NON_START_MINUS, NIL_POINTER );
  133.                         break;
  134.                     }
  135.                     break;
  136.                 default:
  137.                     *success = 0;
  138.                     Alert( BAD_CHARACTER, NIL_POINTER );
  139.                     break;
  140.             }
  141.         }
  142.         else  {
  143.             switch ( temp_char )  {
  144.                 case ZERO:
  145.                     exp_size++;
  146.                     exp_fig[ exp_size ] = 0;
  147.                     break;
  148.                 case ONE:
  149.                     exp_size++;
  150.                     exp_fig[ exp_size ] = 1;
  151.                     break;
  152.                 case TWO:
  153.                     exp_size++;
  154.                     exp_fig[ exp_size ] = 2;
  155.                     break;
  156.                 case THREE:
  157.                     exp_size++;
  158.                     exp_fig[ exp_size ] = 3;
  159.                     break;
  160.                 case FOUR:
  161.                     exp_size++;
  162.                     exp_fig[ exp_size ] = 4;
  163.                     break;
  164.                 case FIVE:
  165.                     exp_size++;
  166.                     exp_fig[ exp_size ] = 5;
  167.                     break;
  168.                 case SIX:
  169.                     exp_size++;
  170.                     exp_fig[ exp_size ] = 6;
  171.                     break;
  172.                 case SEVEN:
  173.                     exp_size++;
  174.                     exp_fig[ exp_size ] = 7;
  175.                     break;
  176.                 case EIGHT:
  177.                     exp_size++;
  178.                     exp_fig[ exp_size ] = 8;
  179.                     break;
  180.                 case NINE:
  181.                     exp_size++;
  182.                     exp_fig[ exp_size ] = 9;
  183.                     break;
  184.                 case MINUS:
  185.                     if ( exp_size == 0 )
  186.                         exp_neg = 1;
  187.                     else  {
  188.                         *success = 0;
  189.                         Alert( EXP_NON_START_MINUS, NIL_POINTER );
  190.                         break;
  191.                     }
  192.                     break;
  193.                 case SPACE:
  194.                     continue;
  195.                     break;
  196.                 case POINT:
  197.                     *success = 0;
  198.                     Alert( DEC_PT_IN_EXP, NIL_POINTER );
  199.                     break;
  200.                 case COMMA:
  201.                     continue;
  202.                     break;
  203.                 default:
  204.                     *success = 0;
  205.                     Alert( BAD_CHAR, NIL_POINTER );
  206.                     break;
  207.             }
  208.         }
  209.         
  210.         if ( !*success )  i = temp + 2;
  211.     }
  212.     
  213.     if ( *success ) {
  214.     
  215.         if ( dec_found == 0 ) 
  216.             dec_pos = sig_size;
  217.             
  218.         k = 1;
  219.                 
  220.         if ( exp_start )  {
  221.             for ( j = exp_size; j > 0; j-- )  {
  222.             
  223.                 if ( j > 1 )
  224.                     exp_value += exp_fig[ k ] * i_pow( 10, j - 1 );
  225.                 else
  226.                     exp_value += exp_fig[ k ];
  227.                 
  228.                 k++;
  229.             }
  230.         }
  231.         
  232.         if ( exp_neg )
  233.             exp_value *= - 1;
  234.         
  235.         abs_place = dec_pos + exp_value - 1;
  236.         abs_p_d = abs_place * 1.0;
  237.         
  238.         *return_number = 0.0;
  239.         
  240.         for( i = 1; i <= sig_size; i++ )  {
  241.             
  242.             if ( abs_p_d > 0 )
  243.                 *return_number += sig_fig[ i ] * ext_pow( 10.0, abs_p_d ) ;
  244.             else if ( abs_p_d == 0 )
  245.                 *return_number += sig_fig[ i ];
  246.             else 
  247.                 *return_number += sig_fig[ i ] / ext_pow( 10.0, - abs_p_d ) ;
  248.                 
  249.             abs_p_d--;
  250.         }
  251.         
  252.         if ( sig_neg )
  253.             *return_number *= - 1.0 ;
  254.     }
  255. }
  256.  
  257. /*******************************************/        
  258.         
  259. int        i_pow( x, y )
  260. int        x, y;
  261. {
  262.     int        i, j, value;
  263.     
  264.     value = x;
  265.     
  266.     for ( i = 2; i <= y; i++ ) 
  267.         value *= x;
  268.         
  269.     return (value);
  270. }
  271.  
  272.